/* RequisicaoSaidaDAO.java
 * Criado em 28/10/2005.
 */
package br.com.werp.regras.almoxarifado.integracao;

import br.com.werp.regras.geral.integracao.MasterDAO;
import br.com.werp.regras.geral.interfaces.algoritimo.IdVirtual;
import br.com.werp.regras.geral.util.Constantes;
import br.com.werp.regras.geral.util.Dia;
import br.com.werp.regras.geral.util.Util;
import br.com.werp.regras.geral.vos.almoxarifado.SaidaSaida_ItemVO;
import br.com.werp.regras.geral.vos.tabelas.Saida;
import br.com.werp.regras.geral.vos.tabelas.Saida_item;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;


/**
 * @author Paulo Sobreira
 * @author Rafael Carneiro (<a href="mailto:rafael@portaljava.com">e-mail</a>)
 *
 */
public class RequisicaoSaidaDAO extends MasterDAO {
    private static RequisicaoSaidaDAO requisicaoSaidaDAO;
    public static final String APAGAR_SAIDA_ITEM = "APAGAR_SAIDA_ITEM";
    public static final String PESQUISAR_REQUISITOR = "PESQUISAR_REQUISITOR";

    private RequisicaoSaidaDAO(String banco) throws Exception {
        super(banco);
    }

    public static RequisicaoSaidaDAO getRequisicaoSaidaDAO()
        throws Exception {
        if (requisicaoSaidaDAO == null) {
            ResourceBundle bundle = ResourceBundle.getBundle(Constantes.CONFIG);
            requisicaoSaidaDAO = new RequisicaoSaidaDAO(bundle.getString(
                        Constantes.BANCO));
        }

        return requisicaoSaidaDAO;
    }

    public void inserirSaida(Object key, Saida saida) throws Exception {
        Connection connection = validarTransacao(key);
        inserirVO(connection, saida);
    }

    public void inserirSaidaItem(Object key, Saida_item saida_item)
        throws Exception {
        Connection connection = validarTransacao(key);
        inserirVO(connection, saida_item, new String[] { "sdit_id" });
    }

    public List obterSaidasItensVO(Object key, SaidaSaida_ItemVO itensVO)
        throws Exception {
        Connection connection = (Connection) (key);

        try {
            String where = " sdit_saida = said_num ";

            if (itensVO.getSaid_num() != 0) {
                where += (" and said_num = " + itensVO.getSaid_num());
            }

            return preencheVO(connection, itensVO, "saida_item,saida", where,
                IdVirtual.EXCLUIDAS);
        } finally {
            if (connection.getAutoCommit()) {
                liberaConeccao(connection);
            }
        }
    }

    public void apagarSaidasItens(Object key, SaidaSaida_ItemVO itemVO)
        throws Exception {
        Connection connection = validarTransacao(key);

        String query = getQuery(APAGAR_SAIDA_ITEM);

        PreparedStatement statement = connection.prepareStatement(query);

        setInt(1, statement, itemVO.getSdit_id());

        statement.executeUpdate();
    }

    public int pesquisarUltimaRequisicao(String usuario)
        throws Exception {
        Connection connection = getConexao();

        try {
            int max = 0;
            String query = getQuery(PESQUISAR_REQUISITOR);
            PreparedStatement statement = connection.prepareStatement(query);
            setString(1, statement, usuario);

            ResultSet set = statement.executeQuery();

            while (set.next()) {
                max = set.getInt("maximus");
            }

            return max;
        } finally {
            liberaConeccao(connection);
        }
    }

    public List obterSaidas(Saida saida,Dia inicio,Dia fim) throws Exception {
        Connection connection = getConexao();

        if ("".equals(saida.getSaid_status())) {
            saida.setSaid_status(null);
        }

        if ("".equals(saida.getSaid_requisitor())) {
            saida.setSaid_requisitor(null);
        }

        String where = "";

        if (saida.getSaid_data() != null) {
            where += (" and said_data like " +
            new Dia(saida.getSaid_data()).getDataBanco());
            saida.setSaid_data(null);
        }

        if (inicio != null) {
            where += (" and said_data >= " + inicio.getDataBanco());
        }
        if (fim != null) {
            where += (" and said_data <= " + fim.getDataBanco());
        }
        try {
            return pesquisarVO(connection, saida, where);
        } finally {
            liberaConeccao(connection);
        }
    }

    public Saida obterSaida(Saida saida) throws Exception {
        return (Saida) Util.get0(obterSaidas(saida));
    }

    private List obterSaidas(Saida saida) throws Exception {
       return obterSaidas(saida, null, null);
    }

    public List obterSaidasItensVO(SaidaSaida_ItemVO itemVO)
        throws Exception {
        return obterSaidasItensVO(getConexao(), itemVO);
    }

    public void atualizarStatusSaida(Object key, Saida saida)
        throws Exception {
        Connection connection = validarTransacao(key);
        atualizarVO(connection, saida, new String[] { "said_num" },
            Arrays.asList(new String[] { "said_status" }));
    }
}
